define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'card/index',
                    add_url: 'card/add',
                    edit_url: 'card/edit',
                    del_url: 'card/del',
                    multi_url: 'card/multi',
                    table: 'card',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-add").data("area", ["90%", "90%"]);
                $(".btn-editone").data("area", ["90%", "90%"]);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('排序'), sortable: true},
                        {field: 'company.company_name', title: '公司昵称', operate: "like"},
                        {field: 'company.company_code', title: '公司编码', operate: "like"},
                        {field: 'card_no', title: '卡号', operate: "like"},
                        {field: 'money', title: '面值', operate: false},
                        {
                            field: 'status', title: __('核销状态'),
                            searchList: {
                                "1": __('已核销'),
                                "0": __('未核销'),
                            },
                            formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        return '<span style="color:red">已核销</span>';
                                        break;
                                    case 0:
                                        return '<span style="color:blue">未核销</span>';
                                        break;

                                }
                                return value;
                            }
                        },
                        {
                            field: 'card_status', title: __('状态'),
                            searchList: {
                                "1": __('有效'),
                                "0": __('无效'),
                            },
                            formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        return '<span style="color:red">有效</span>';
                                        break;
                                    case 0:
                                        return '<span style="color:blue">无效</span>';
                                        break;

                                }
                                return value;
                            }
                        },
                        {
                            field: 'usetime',
                            title: __('核销时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'createtime',
                            title: __('添加时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-export1", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                console.log(ids.length, page.length, all);
                Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("domain/export1") + "' method='post' id='cc' target='_blank'><input type='hidden' name='ids' id='kk' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
                    title: '导出数据',
                    btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    }
                    , yes: function (index, layero) {
                        if (ids.length > 0) {
                            var form = document.getElementById('cc');
                            var custmoreInput = document.getElementById('kk');
                            custmoreInput.setAttribute("value", ids.join(","));
                            form.submit(ids.join(","), layero);
                            return true;
                        }
                        return false;
                    }
                    ,
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        if (ids.length > 0) {
                            var form = document.getElementById('cc');
                            var custmoreInput = document.getElementById('kk');
                            custmoreInput.setAttribute("value", ids.join(","));
                            form.submit(ids.join(","), layero);
                            return true;
                        }
                        return false;
                    }
                    ,
                    btn3: function (index, layero) {
                        var form = document.getElementById('cc');
                        var custmoreInput = document.getElementById('kk');
                        custmoreInput.setAttribute("value", "all");
                        form.submit("all", layero);
                        return true;
                    }
                })
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        reset: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});